#!/usr/bin/env python
# encoding: utf-8
import re
import datetime
import time, random
from functools import reduce


from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def time_fix(time_string):
    now_time = datetime.datetime.now()
    if '分钟前' in time_string:
        minutes = re.search(r'^(\d+)分钟', time_string).group(1)
        created_at = now_time - datetime.timedelta(minutes=int(minutes))
        return created_at.strftime('%Y-%m-%d %H:%M')

    if '小时前' in time_string:
        minutes = re.search(r'^(\d+)小时', time_string).group(1)
        created_at = now_time - datetime.timedelta(hours=int(minutes))
        return created_at.strftime('%Y-%m-%d %H:%M')

    if '今天' in time_string:
        return time_string.replace('今天', now_time.strftime('%Y-%m-%d'))

    if '月' in time_string:
        time_string = time_string.replace('月', '-').replace('日', '')
        time_string = str(now_time.year) + '-' + time_string
        return time_string

    return time_string


keyword_re = re.compile('<span class="kt">|</span>|原图|<!-- 是否进行翻译 -->|<span class="cmt">|\[组图共.张\]')
emoji_re = re.compile('<img alt="|" src="//h5\.sinaimg(.*?)/>')
white_space_re = re.compile('<br />')
div_re = re.compile('</div>|<div>')
image_re = re.compile('<img(.*?)/>')
url_re = re.compile('<a href=(.*?)>|</a>')


def extract_weibo_content(weibo_html):
    s = weibo_html
    if 'class="ctt">' in s:
        s = s.split('class="ctt">', maxsplit=1)[1]
    s = emoji_re.sub('', s)
    s = url_re.sub('', s)
    s = div_re.sub('', s)
    s = image_re.sub('', s)
    if '<span class="ct">' in s:
        s = s.split('<span class="ct">')[0]
    splits = s.split('赞[')
    if len(splits) == 2:
        s = splits[0]
    if len(splits) == 3:
        origin_text = splits[0]
        retweet_text = splits[1].split('转发理由:')[1]
        s = origin_text + '转发理由:' + retweet_text
    s = white_space_re.sub(' ', s)
    s = keyword_re.sub('', s)
    s = s.replace('\xa0', '')
    s = s.strip(':')
    s = s.strip()
    return s


def extract_comment_content(comment_html):
    s = comment_html
    if 'class="ctt">' in s:
        s = s.split('class="ctt">', maxsplit=1)[1]
    s = s.split('举报', maxsplit=1)[0]
    s = emoji_re.sub('', s)
    s = keyword_re.sub('', s)
    s = url_re.sub('', s)
    s = div_re.sub('', s)
    s = image_re.sub('', s)
    s = white_space_re.sub(' ', s)
    s = s.replace('\xa0', '')
    s = s.strip(':')
    s = s.strip()
    return s

def get_news_calorificvalue(news_title):
    chromedriver = r'D:\OneDriveEdu\file\project\grpc_w2m_framework_m\worker\yuqing\crawler\spider\chromedriver.exe'
    # chromedriver = './chromedriver'      # Linux端Chrome浏览器
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    browser = webdriver.Chrome(chrome_options=chrome_options, executable_path=chromedriver) # 1.创建Chrome浏览器对象
    browser.get('https://cn.bing.com')
    # browser.find_element_by_css_selector('#est_en').click()
    time.sleep(random.random())
    browser.find_element_by_css_selector('#sb_form_q').send_keys(news_title)     # 输入要搜索的字符串
    time.sleep(random.random())
    # browser.find_element_by_css_selector('#sb_form > label').click()            # 点击搜索
    browser.find_element_by_css_selector('#sb_go_par').click()            # 点击搜索
    # print(browser.page_source)
    news_calorificvalue = browser.find_element_by_class_name('sb_count').text
    # print("ssssssss",news_calorificvalue)
    news_calorificvalue = str(news_calorificvalue).replace(",","").replace("条结果", "")
    # print(news_calorificvalue)
    return news_calorificvalue

def dict_reduce(arr):
    f = lambda x, y: x if y in x else x + [y]
    return reduce(f, [[], ] + arr)

def getSentimentResults(senta, comment):
    # 指定模型输入
    input_dict = {"text": [comment]}
    # 把数据喂给senta模型的文本分类函数
    results = senta.sentiment_classify(data=input_dict)
    positive_probs = results[0]['positive_probs']
    return positive_probs


if __name__ == '__main__':
    arr = [{'text': 'wuyuan', 'value': 1}, {'text': '默认', 'value': 2}, {'text': '默认', 'value': 2},
           {'text': 'wyy', 'value': 4}]
    f = lambda x, y: x if y in x else x + [y]
    arr = reduce(f, [[], ] + arr)
    print(arr)







